#include "../sne/sne/NetUtil.h"
//#include "../sne/sne/UdpPeer.h"
#include "../sne/sne/SocketHandler.h"
#include "../sne/sne/Packet.h"
#include "../sne/sne/PacketFactoryMgr.h"
#include "../sne/sne/TcpAcceptor.h"
#include "../packets.h"
#include "../file/File.h"
#include "../sne/sne/UdpPeer.h"

#include <time.h>


class ServerHandler : public SocketHandler
{
public:
	ServerHandler()
	{
		fp = 0;
		tt = 0;
	}

public:
	void OnSessionOpened(Session* ss)
	{

	}

	void OnSessionClosed(Session* ss)
	{

	}

	void OnMessageRecv( Session* ss, Packet* pkt )
	{
		if (pkt->GetID() == ID_HELLO)
		{
			helloPacket* hh = (helloPacket*)pkt;

			printf("receive : %s, index = [%d], from %s:%d\n", hh->hello, hh->index, ip_i2s(ss->m_remote_ip), ss->m_remote_port);

			strcpy(hh->hello, "pong");

			ss->Write(pkt);
			
		}
		else if (pkt->GetID() == ID_FILE)
		{
			filePacket* fpkt = (filePacket*)pkt;
			if (fp == 0)
			{
				char temp[64];
				sprintf(temp, "C:\\%s", fpkt->filename);
				fp = new File(temp, "wb");
				tt = clock();
				//log("-------------------file : %s transfer begin----------------------", fpkt->filename);
			}

			fp->fwrite(fpkt->buf, 1, fpkt->len);

			//log("file : %s receive %d bytes, block: %d, time: %d", fpkt->filename, fpkt->len, fpkt->block_index, (clock() - tt) / 1000);

			if (fpkt->len < sizeof(fpkt->buf))
			{
				fp->fclose();
				//log("-------------------file : %s transfer finished-------------------", fpkt->filename);

				delete fp;
				fp = 0;

				helloPacket hh;
				strcpy(hh.hello, "file finish");

				ss->Write(&hh);
			}
		}
	}

private:

	File* fp;
	int tt;
};

#if 1 //tcp
int main()
{
	//log("======= Select Model Server ========\n");
	
	SocketHandler* serverHandler = new ServerHandler;

	TcpAcceptor* Server = new TcpAcceptor(serverHandler);
	Server->Bind("0.0.0.0", 5566);

	REGISTER_PACKET(helloPacket);
	
	while(1)
	{
		Server->UpdateAll();
		//Sleep(100);
		//Server->SendData("127.0.0.1",5566,"hello ss",9);
	}
}

#else
//udp
int main()
{
	//log("======= udp Server ========\n");

	SocketHandler* serverHandler = new ServerHandler;

	PacketFactory* serverFactory = new packetsFactory;

	UdpPeer* Server = new UdpPeer(serverHandler,serverFactory);
	Server->Bind("0.0.0.0", 5566);

	while(1)
	{
		Server->UpdateAll();
		//Sleep(100);
		//Server->SendData("127.0.0.1",5566,"hello ss",9);
	}
}
#endif